package com.cn.zy.zheda.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.cn.zy.zheda.demo.controller.response.res.DetailsEntity;
import com.cn.zy.zheda.demo.dao.AlarmDao;
import com.cn.zy.zheda.demo.dao.DeviceDao;
import com.cn.zy.zheda.demo.entity.AlarmEntity;
import com.cn.zy.zheda.demo.entity.AttributeKvEntity;
import com.cn.zy.zheda.demo.entity.DeviceEntity;
import com.cn.zy.zheda.demo.controller.response.Attributekv;
import com.cn.zy.zheda.demo.entity.res.AlarmEntityRes;
import com.cn.zy.zheda.demo.entity.res.AttributeEntity;
import com.cn.zy.zheda.demo.service.AlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zhangyi
 * @date 2018/12/19 17:55
 */
@Service
public class AlarmServiceImpl implements AlarmService {

    @Autowired
    private DeviceDao deviceDao;

    @Autowired
    private AlarmDao alarmDao;

    /**
     * 获取报警详细信息
     *
     * @param customerId
     * @return
     */
    @Override
    public List<Map<String,Object>> info(String customerId) {
        Map<String,Object> deviceMap = new HashMap<>();
        List<Map<String,Object>> alarmList = new ArrayList<>();
        List<DeviceEntity> deviceIds = deviceDao.getDeviceIds(customerId);
        List<Attributekv> attributekvList = new ArrayList<>();
        deviceMap.put("customerId",customerId);
        deviceIds.stream().forEach(e -> {
            String deviceId = e.getId();
            List<AlarmEntity> alarms = alarmDao.getAlarms(deviceId);
            //这里可以获取到报警信息了
            List<AttributeKvEntity> attributeKvs = alarmDao.getAttributeKvs(deviceId);
            attributeKvs.stream().map(AttributeKvEntity::getStrV).forEach(a -> {
                //json转化对象,去除""号重复解析
                Attributekv attributekv = JSON.parseObject(a, Attributekv.class);
                if(!Objects.isNull(attributekv)){
                    attributekvList.add(attributekv);
                }
            });
            deviceMap.put("attributeKv",attributekvList);
            //去空
            if(alarms.size() != 0) {

                deviceMap.put("alarms", alarms);
                alarmList.add(deviceMap);
            }
        });
        return alarmList;
    }

    @Override
    public Map<String, Object> boom(String customerId) {
        //整合对象
        Map<String,Object> response = new HashMap<>();
        response.put("customerId",customerId);
        List<DeviceEntity> deviceIds = deviceDao.getDeviceIds(customerId);
        List<AlarmEntityRes> alarmEntityResList = new ArrayList<>();
        deviceIds.stream().forEach(d -> {
            //device Id
            String id = d.getId();
            //device name
            String deviceName = d.getName();
            List<AlarmEntity> alarms = alarmDao.getAlarms(id);
            List<AttributeEntity> attributeEntityList = new ArrayList<>();
            alarms.stream().forEach((AlarmEntity a) -> {
                String additionalInfo = a.getAdditionalInfo();
                DetailsEntity detailsEntity = JSON.parseObject(additionalInfo, new TypeReference<DetailsEntity>() {});
                String key = detailsEntity.getMessage();
                String status = a.getStatus();
                String type = a.getType();
                String name = null;
                List<AttributeKvEntity> attributeKvs = alarmDao.getAttributeKvs(id);
                for (AttributeKvEntity ak : attributeKvs){
                    String attributeKey = ak.getAttributeKey();
                    String entityId = ak.getEntityId();
                    if(id.equals(entityId) && type.equals(attributeKey)){
                        String strV = ak.getStrV();
                        Attributekv attributekv = JSON.parseObject(strV, Attributekv.class);
                        name = attributekv.getName();
                        break;
                    }
                }
                AttributeEntity attributeE = new AttributeEntity(
                        a.getStartTs(),
                        key,
                        name,
                        a.getAdditionalInfo(),
                        status);
                attributeEntityList.add(attributeE);
            });
            if(attributeEntityList.size() != 0) {
                AlarmEntityRes alarmEntity = new AlarmEntityRes(id, deviceName, attributeEntityList);
                alarmEntityResList.add(alarmEntity);
            }
        });
        response.put("alarm",alarmEntityResList);
        return response;
    }
}
